package com.ruoyi.system.controller.registry;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RegistrationRequest;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.Registry.IRegisrtryRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/registration")
public class RegistryController extends BaseController {

    @Autowired
    private IRegisrtryRequestService registryRequestService;

    @Autowired
    private ISysRoleService roleService;

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(registryRequestService.selectUserRegistrationRequestsById(id));
    }
    @GetMapping("/roleList")
    public AjaxResult getRoleList(){
        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", roles);
        return ajax;
    }

    @PostMapping("/submit")
    //匿名请求
    @Anonymous
    public AjaxResult submitRegistration(@RequestBody RegistrationRequest registrationRequest)
    {

        String msg = registryRequestService.submitRequest(registrationRequest);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
    @Log(title = "同意注册申请", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approveRequest(@RequestBody RegistrationRequest registrationRequest)
    {
        String msg = registryRequestService.approveRequest(registrationRequest);
        System.out.println(registrationRequest);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
    @Log(title = "拒绝注册申请", businessType = BusinessType.UPDATE)
    @PutMapping("/reject")
    public AjaxResult rejectRequest(@RequestBody RegistrationRequest registrationRequest)
    {
        String msg = registryRequestService.rejectRequest(registrationRequest);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }
    @Log(title = "获取注册申请列表")
    @GetMapping("/requestRegistry/list")
    public List<RegistrationRequest> getRegistryRequest(RegistrationRequest registrationRequest)
    {
//        startPage();
        List<RegistrationRequest> list = registryRequestService.selectRegistrationRequestList(registrationRequest);
        return list;
    }



}
